package cn.tedu.sp11.fallback;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;

import cn.tedu.web.util.JsonResult;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class ItemServiceFallback implements FallbackProvider {
    @Override
    public String getRoute() {
        //当执行item-service失败，返回service-id
        //应用当前这个降级类只针对那个服务进行降级
        return "item-service";
        //星号和null都表示所有微服务失败都应用当前降级类
        //"*"; //null;
    }

    //该方法返回封装降级响应的对象
    //ClientHttpResponse中封装降级响应
    @Override
    public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
        //返回降级相应    在response对象中，要设置http协议的状态码，协议头，协议体
        return new ClientHttpResponse() {
            @Override
            public HttpStatus getStatusCode() throws IOException {
                return HttpStatus.OK;
            }

            @Override
            public int getRawStatusCode() throws IOException {
                return HttpStatus.OK.value();
            }

            @Override
            public String getStatusText() throws IOException {
                return HttpStatus.OK.getReasonPhrase();
            }

            @Override
            public void close() {

            }
            //协议头
            @Override
            public InputStream getBody() throws IOException {
                //JsonResult--0{code:200,msg:"调用商品服务失败",data:null}
                String json = JsonResult.err().msg("调用商品服务失败").toString();
                return new ByteArrayInputStream(json.getBytes());
            }
            //协议体
            @Override
            public HttpHeaders getHeaders() {
                //content-type:application/json;charset=utf-8
                HttpHeaders httpHeaders = new HttpHeaders();
                httpHeaders.setContentType(MediaType.APPLICATION_JSON);
                return httpHeaders;
            }
        };
//        return response();
    }

//    private ClientHttpResponse response() {
//        return new ClientHttpResponse() {
//            //下面三个方法都是协议号
//            @Override
//            public HttpStatus getStatusCode() throws IOException {
//                return HttpStatus.OK;
//            }
//            @Override
//            public int getRawStatusCode() throws IOException {
//                return HttpStatus.OK.value();
//            }
//            @Override
//            public String getStatusText() throws IOException {
//                return HttpStatus.OK.getReasonPhrase();
//            }
//
//            @Override
//            public void close() {
//            }
//
//            @Override
//            public InputStream getBody() throws IOException {
//                log.info("fallback body");
//                String s = JsonResult.err().msg("后台服务错误").toString();
//                return new ByteArrayInputStream(s.getBytes("UTF-8"));
//            }
//
//            @Override
//            public HttpHeaders getHeaders() {
//                HttpHeaders headers = new HttpHeaders();
//                headers.setContentType(MediaType.APPLICATION_JSON);
//                return headers;
//            }
//        };
//    }
}
